<?php
namespace app\logic\yuql;

use app\api\model\yuql\Login;
use app\api\model\yuql\Wxlogin;
use app\services\JwtServices;
use app\services\yuql\SmsServices;
use app\validate\yuql\LoginValidate;
use app\validate\yuql\RegisterValidate;
use app\validate\yuql\SmsSendValidate;
use think\facade\Cache;

class LoginLogic{

    /**
     * yuql
     * Vue登录逻辑
     * @param $params
     * @return string
     */
    public static function logins($params){
        //验证手机号密码非空                      //抛出异常
        if (empty($params['phone']) || empty($params['password'])) throw new \Exception('手机号和密码不能为空');
        //手机号正则验证
        $regular = '/^1[35789]\d{9}$/';
        if (!preg_match($regular,$params['phone'])){
            throw new \Exception('手机号格式不正确');
        }
        //根据手机号查询用户
        $userInfo = Login::where('phone',$params['phone'])->find();
        //无此用户抛出异常
        if (empty($userInfo)) throw new \Exception('查无此人');
        //有此用户进行密码验证
        if ($params['password'] != unlock_url($userInfo['password'])) throw new \Exception('密码错误');
        $res = JwtServices::JwtEncode($userInfo->uid);
        return $res;
    }


    /**
     * yuql
     * 后台登录逻辑
     * 查询一整条数据
     * 密码验证
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function wxlogins($params){
        validate(LoginValidate::class)->check($params);
        $userInfo=Wxlogin::getUserOne(['phone'=>$params['phone']]);
        if (empty($userInfo)) throw new \Exception('查无此人');
        //有此用户进行密码验证
        if ($params['password'] != unlock_url($userInfo['password'])) throw new \Exception('密码错误');
        $token = JwtServices::JwtEncode($userInfo->uid);
        $arr = [];
        $arr['id'] = $userInfo->id;
        $arr['phone'] = $userInfo->phone;
        $arr['token'] = $token;
        return $arr;
    }

    /**
     * yuql
     * 发送验证码
     * @param $phone
     * @return int
     * @throws \Exception
     */
    public static function loginSend($phone){
        if (!$phone) throw new \Exception('手机号不能为空');
        $code = mt_rand(1000,9999);
        $content = '您的验证码是' . $code;
        $res = SmsServices::send($phone,$content,true);
        if (!$res) throw new \Exception('验证码发送失败');
        $sendCodeName = 'code' . $code;
        $sendPhoneName = 'phone' . $phone;
        if ($res['status'] == 1){
            Cache::set($sendCodeName,$code,600); //验证码入缓存
            Cache::set($sendPhoneName,$phone,600); //手机号入缓存
            return $code;
        }else{
            throw new \Exception('发送失败');
        }
    }

    /**
     * yuql
     * 获取验证码注册用户
     * @param $params
     * @return Wxlogin|\think\Model
     * @throws \Exception
     */
    public static function register($params){
        validate(RegisterValidate::class)->check($params);
        $sendCode = 'code' . $params['code'];
        $sendPhone = 'phone' . $params['phone'];
        $sendCodeName = Cache::get($sendCode);
        if ($params['code'] != $sendCodeName) throw new \Exception('验证码不正确');
        $sendPhoneName = Cache::get($sendPhone);
        if ($params['phone'] != $sendPhoneName) throw new \Exception('手机号不正确');

        $userInfo = Wxlogin::getUserOne(['phone'=>$params['phone']]);
        if ($userInfo) throw new \Exception('已有该用户');
        $res = Wxlogin::getLogon(['phone'=>$params['phone']]);
        if (!$res) throw new \Exception('注册失败');
        return $res;
    }

    /**
     * yuql
     * 注册后设置密码
     * @param $params
     * @return Wxlogin|array|mixed|\think\Model|null
     * @throws \Exception
     */
    public static function setPassword($params){
        validate(LoginValidate::class)->check($params);
        $userInfo = Wxlogin::getUserOne(['phone'=>$params['phone']]);
        if (!$userInfo) throw new \Exception('用户未注册');
        $res = Wxlogin::getPassword(['phone'=>$params['phone']],$params['password']);
        return $res;
    }
}